﻿@inherits RazorFunction
@using System.Xml
@using System.Xml.Linq;
@using Composite.Core
@functions {
    public override string FunctionDescription
    {
        get { return "A statement page block"; }
    }

    [FunctionParameter(Label = "Statement content")]
    public XhtmlDocument Statement { get; set; }

    [FunctionParameter(Label = "Background Style", DefaultValue = "none", WidgetFactoryMethod = "GetStyleWidget")]
    public string Style { get; set; }
}

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:f="http://www.composite.net/ns/function/1.0">
<head>
</head>
<body>
    <div class="@Style page-block page-block-statement">
        <div class="container">
            @Markup(Statement)
        </div>
    </div>
</body>
</html>


@functions {
    public static WidgetFunctionProvider GetStyleWidget()
    {
        Expression<Func<object>> expression = () => GetBackgroundOptions();
        var method = (expression.Body as MethodCallExpression).Method;

        return StandardWidgetFunctions.DropDownList(method.DeclaringType, method.Name, "Key", "Value", false);
    }

    public static Dictionary<string, string> GetBackgroundOptions()
    {
        var options = new Dictionary<string, string>();
        try
        {
            var optionsFile = XDocument.Load(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"App_Data/Razor/PageBlocks/BackgroundStyleOptions.xml"));
            options = optionsFile.Root.Elements("Option").ToDictionary(el => el.Attribute("CssClassKey").Value, el => el.Attribute("Label").Value);
        }
        catch
        {
            Log.LogError("PageBlocks.Statement", "Error while loading options for the Background parameter");
        }
        return options;
    }

}
